const validate = (schema) => {
  return (req, res, next) => {
    const { error, value } = schema.validate(req.body, {
      abortEarly: false,
      stripUnknown: true,
      convert: true
    });
    
    if (error) {
      const message = error.details.map(detail => detail.message).join(', ');
      return res.status(400).json({
        success: false,
        message: '验证失败',
        errors: message
      });
    }
    
    // 使用验证后的值替换原始请求体
    req.body = value;
    next();
  };
};

module.exports = validate;